package com.hefei.customer.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hefei.api.customer.vo.Customer;
import com.hefei.api.customer.vo.easyui.Json;
import com.hefei.common.request.RequestThreadLocal;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.customer.service.ICustomerService;





@Controller
@RequestMapping("/customer")
@SuppressWarnings("all")
public class CustomerController {
	
	private Logger logger = Logger.getLogger(CustomerController.class);
	@Autowired
	private ICustomerService customerService;

	/**
	 * 跳转到客户表格页面
	 * @return
	 */
	@RequestMapping(value = "/customer/list",method = RequestMethod.GET)
    public String customerList(Model model) {
        return "crm/customer_list";
    }
	/**
	 * 保存前台相关客户信息
	 * @return  j
	 */
	@ResponseBody
	@RequestMapping(value="/addCustomer",method=RequestMethod.POST)
	public Json saveCustomerInfo(HttpServletRequest request,HttpServletResponse response){

		logger.info(RequestThreadLocal.getLoggerStr()+"增加customer");
		
		Json j = null;
		try {
			String name = StringUtils.trim(request.getParameter("name"));
			String address = StringUtils.trim(request.getParameter("address"));
			String zipcode = StringUtils.trim(request.getParameter("zipcode"));
			String industry = StringUtils.trim(request.getParameter("industry"));
			String annualrev =  StringUtils.trim(request.getParameter("annualrev"));
			
			Customer customerRequest = new Customer();
			customerRequest.setName(name);
			customerRequest.setAddress(address);
			customerRequest.setZipcode(zipcode);
			customerRequest.setIndustry(industry);
			customerRequest.setAnnualrev(annualrev);
			
			String returnCode = customerService.saveCustomerInfo(customerRequest);
			if (returnCode.equals(ReturnCode.RETURN_CODE_SUCCESS)) {
				j.setSuccess(true);
				j.setMsg("用户新增成功！");
				j.setObj(customerRequest);	
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
		
	}
	
	/**
     * 修改客户信息
     * 
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "/editCustomer",method = RequestMethod.POST)
	public Json editCustomerInfo(Customer customer){
		 
		 	logger.info(RequestThreadLocal.getLoggerStr()+"增加customer");
			
			Json j = null;
	        try {
	        	String returnCode = customerService.editCustomerInfo(customer);
	        	if (returnCode.equals(ReturnCode.RETURN_CODE_SUCCESS)) {
					
	        		j.setSuccess(true);
	        		j.setMsg("编辑成功！");
	        		j.setObj(customer);
				}
	        } catch (Exception e) {
	            j.setMsg(e.getMessage());
	        }
	        return j;
		
	}
	
	/**
     * 删除客户信息
     * 
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "/deleteCustomer",method = RequestMethod.POST)
	public Json deleteCustomerInfo(Customer customer){
		
		logger.info(RequestThreadLocal.getLoggerStr()+"增加customer");
		
		Json j = null;
        try {
        	String returnCode = customerService.deleteCustomerInfo(customer.getCustomerId());
        	if (returnCode.equals(ReturnCode.RETURN_CODE_SUCCESS)) {
        		j.setSuccess(true);
        		j.setMsg("删除成功！");
				
			}
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
		
	}
	
	
	
	

}
